package com.hd123.rumba.gwt.widget.client.grid;

import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.Widget;

public class DxNavGrid extends DockPanel implements KeyboardListener {
  DxNavigator navigator;
  DxGrid grid;

  public DxNavGrid(DxGridDataProvider provider, DxGrid grid) {
    super();
    this.grid = grid;
    navigator = new DxNavigator(provider, grid);
    add(navigator, DockPanel.SOUTH);
    add(grid, DockPanel.CENTER);
    grid.addKeyboardListener(this);
  }

  public void gotoPage(int page) {
    navigator.gotoPage(page);
  }


  public DxGrid getGrid() {
    return grid;
  }

  public DxNavigator getNavigator() {
    return navigator;
  }

  public void onKeyDown(Widget sender, char keyCode, int modifiers) {
    if (sender == grid && grid.isReadOnly()) {
      if (keyCode == KEY_PAGEUP) {
        if ((modifiers & MODIFIER_CTRL) != 0)
          navigator.onClick(navigator.first);
        else
          navigator.onClick(navigator.prior);
        grid.cancelKey();
      } else if (keyCode == KEY_PAGEDOWN) {
        if ((modifiers & MODIFIER_CTRL) != 0)
          navigator.onClick(navigator.last);
        else
          navigator.onClick(navigator.next);
        grid.cancelKey();
      }
    }
  }

  public void onKeyPress(Widget sender, char keyCode, int modifiers) {

  }

  public void onKeyUp(Widget sender, char keyCode, int modifiers) {

  }
}
